package com.lxr0860.lxr0860production_test.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lxr0860.lxr0860production_test.R;
import com.lxr0860.lxr0860production_test.utils.AppCacheUtils;
import com.lxr0860.lxr0860production_test.utils.Contants;
import com.lxr0860.lxr0860production_test.utils.FolderType;

import java.io.File;
import java.util.List;

public class PhotoAdapter extends BaseAdapter {

    private List<String> photos;
    private Context context;

    public PhotoAdapter(Context context, List<String> photos){
        this.context=context;
        this.photos=photos;
    }

    @Override
    public int getCount() {
        return photos.size();
    }

    @Override
    public Object getItem(int position) {
        return photos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder=null;
        if(convertView==null){
            convertView= LayoutInflater.from(context)
                    .inflate(R.layout.item_photo_layout,parent,false);
            holder=new Holder();
            holder.photo= (ImageView) convertView.findViewById(R.id.iv_item_photo);
            holder.name= (TextView) convertView.findViewById(R.id.tv_item_photo_name);
            convertView.setTag(holder);
        }else {
            holder= (Holder) convertView.getTag();
        }
        setPhotos(holder.photo,photos.get(position));
        holder.name.setText(photos.get(position));
        return convertView;
    }

    class Holder{
        ImageView photo;
        TextView name;
    }

    private void setPhotos(ImageView view, String fileName){
//        BitmapFactory.Options options=new BitmapFactory.Options();
        String imagePath =  AppCacheUtils.getPathByFolderType(FolderType.images) +"/"+ fileName;

//        options.inJustDecodeBounds=true;
//        BitmapFactory.decodeFile(
//                Contants.PHOTO_PATH + File.separator + fileName,options);
//        options.inJustDecodeBounds=false;
//        int width=options.outWidth;
//        int height=options.outHeight;
//        int zoomWidth=view.getWidth();
//        int zoomHeight=view.getHeight();
//        float rateW=width*0.1f/zoomWidth;
//        float rateH=height*01f/zoomHeight;
//        int max = (int) Math.ceil(Math.max(rateW, rateH));
//        int sampleSize=1;
//        if(max>sampleSize){
//            sampleSize=max;
//        }
//        options.inSampleSize=sampleSize;
//        Bitmap bitmap= BitmapFactory.decodeFile(
//                Contants.PHOTO_PATH + File.separator + fileName,options);
//        view.setImageBitmap(bitmap);
        view.setRotation(-90);
        view.setImageURI(Uri.fromFile(new File(imagePath)));
    }
}
